package nautilus.moneykeeper;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import nautilus.moneykeeper.dao.AccountDAO;
import nautilus.moneykeeper.dao.DataFileUtils;
import nautilus.moneykeeper.dto.Account;
import nautilus.moneykeeper.dto.Transaction;
import nautilus.moneykeeper.system.Constant;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;

public class SettingActivity extends Activity implements OnClickListener{
	
ProgressDialog progessDlg;
	
	Handler _handler = new Handler(new Handler.Callback() {
		@Override
		public boolean handleMessage(Message msg) {
			switch(msg.what){
				case Constant.MSG_LOAD_DATA_DONE:
					SettingActivity.this.progessDlg.dismiss();
					break;
					
				case Constant.MSG_LOAD_DATA_ERROR:
					SettingActivity.this.progessDlg.dismiss();
					break;
			}
			return false;
		}
	});
	
	public void onCreate(Bundle savedState){
		super.onCreate(savedState);
		
		setContentView(R.layout.setting);
	}
	
	protected void onPause(){
		if(progessDlg != null && progessDlg.isShowing())
			progessDlg.dismiss();
		super.onPause();
	}
	
	@Override
	public void onClick(View v) {
		switch(v.getId()){
			case R.id.btn_export_csv:
				if(progessDlg == null)
					progessDlg = new ProgressDialog(SettingActivity.this);
				progessDlg.show();
				Thread t = new Thread(new Runnable(){
					public void run(){
						SimpleDateFormat format = new SimpleDateFormat("ddMMyyyyhhmmss");
						String timeString = format.format(new Date());
						File file = new File(Constant.BACKUP_STORE);
					    if (!file.exists() ) {
					        //Log.e("Setting", "Directory not created");
					    	//_handler.sendEmptyMessage(Constant.MSG_LOAD_DATA_ERROR);
					        //return;
					        file.mkdirs();
					    }
						
						ArrayList<Account> allAccounts;
						ArrayList<Transaction> allTrans;
						AccountDAO dao;
						
						try {
							dao = new AccountDAO(SettingActivity.this);
							allAccounts = dao.getAllAccounts();
							allTrans = dao.getAllTransactions();
							dao.close();
							DataFileUtils.exportAccountsToCSV(allAccounts, file.getAbsolutePath() + "/accounts" + timeString + ".csv");
							DataFileUtils.exportTransactionsToCSV(allTrans, file.getAbsolutePath() + "/trans" + timeString + ".csv");
							_handler.sendEmptyMessage(Constant.MSG_LOAD_DATA_DONE);
						} catch (IOException e) {
							//e.printStackTrace();
							_handler.sendEmptyMessage(Constant.MSG_LOAD_DATA_ERROR);
						}
					}
				});
				t.start();
			break;
			
			case R.id.btn_home_menu_slide:
				finish();
				break;
		}
	}

	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		finish();
	}
}
